﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;
    using WMS6.Web.UserControls;

    public class MovementList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnMove;
        protected Button btnNewSearch;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected HtmlForm F5212;
        protected GridView gvMovement;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRowIndex;
        protected Label lblMovement;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2MovementList(InvLotLocLpnQueryEntity invLotLocLpnQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(invLotLocLpnQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = InventoryMgtServiceClientProxy.GetInstance().GetInvLotLocLpnByQueryEntity(invLotLocLpnQuery);
            base.BindData2GridView(this.gvMovement, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnQuery = this.GetInvLotLocLpnQueryEntity();
            invLotLocLpnQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = invLotLocLpnQuery.CurrentPage;
            invLotLocLpnQuery.CurrentPage = "0";
            this.gvMovement.AllowSorting = false;
            this.BindData2MovementList(invLotLocLpnQuery);
            this.hdnActionID.Value = "";
            this.gvMovement.ShowFooter = false;
            invLotLocLpnQuery.IsGetAll = false;
            invLotLocLpnQuery.CurrentPage = currentPageIndex;
            this.SetInvLotLocLpnQueryEntity(invLotLocLpnQuery);
            base.Export2Excel("MovementList.xls", this.gvMovement);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnQuery = this.GetInvLotLocLpnQueryEntity();
            invLotLocLpnQuery.CurrentPage = "0";
            this.BindData2MovementList(invLotLocLpnQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnQuery = this.GetInvLotLocLpnQueryEntity();
            invLotLocLpnQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2MovementList(invLotLocLpnQuery);
        }

        protected void btnMove_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> SkuIDs = base.GetSelectedIDsInGridView(this.gvMovement, "SKU_ID");
                if ((SkuIDs == null) || (SkuIDs.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    List<InvMovesInfo> invMovesInfo = new List<InvMovesInfo>();
                    InvMovesInfo invMoves = null;
                    for (int i = 0; i < this.gvMovement.Rows.Count; i++)
                    {
                        if (((CheckBox) this.gvMovement.Rows[i].FindControl("chbSelect")).Checked)
                        {
                            if (!string.IsNullOrEmpty(this.gvMovement.DataKeys[i]["HOLD_STATUS"].ToString()) && (this.gvMovement.DataKeys[i]["HOLD_STATUS"].ToString().ToUpper() != "HOLD"))
                            {
                                invMoves = new InvMovesInfo {
                                    FromLoc = this.gvMovement.DataKeys[i]["LOC"].ToString(),
                                    FromLot = this.gvMovement.DataKeys[i]["LOT"].ToString(),
                                    FromLpn = this.gvMovement.DataKeys[i]["LPN_ID"].ToString(),
                                    FromOwnerID = this.gvMovement.DataKeys[i]["OWNER_ID"].ToString(),
                                    FromSkuID = this.gvMovement.DataKeys[i]["SKU_ID"].ToString(),
                                    ToOwnerID = this.gvMovement.DataKeys[i]["OWNER_ID"].ToString(),
                                    ToSkuID = this.gvMovement.DataKeys[i]["SKU_ID"].ToString(),
                                    ToLot = this.gvMovement.DataKeys[i]["LOT"].ToString(),
                                    ToLoc = ((TextBox) this.gvMovement.Rows[i].FindControl("txtToLoc")).Text.Trim(),
                                    ToLpn = ((TextBox) this.gvMovement.Rows[i].FindControl("txtToLpn")).Text.Trim(),
                                    PackID = ((TextBox) this.gvMovement.Rows[i].FindControl("txtPackID")).Text.Trim(),
                                    UomID = ((DropDownList) this.gvMovement.Rows[i].FindControl("ddlUomID")).SelectedValue.Trim(),
                                    MoveQty = decimal.Parse((((TextBox) this.gvMovement.Rows[i].FindControl("txtMoveQty")).Text.Trim() == "") ? "0" : ((TextBox) this.gvMovement.Rows[i].FindControl("txtMoveQty")).Text.Trim()),
                                    FromMoveQty = decimal.Parse((((TextBox) this.gvMovement.Rows[i].FindControl("txtMoveQty")).Text.Trim() == "") ? "0" : ((TextBox) this.gvMovement.Rows[i].FindControl("txtMoveQty")).Text.Trim()),
                                    UpdatedBy = base.UserInfo.UserID
                                };
                                if (invMoves.MoveQty == 0M)
                                {
                                    this.lblMsg.Text = base.GetGlobalMsgResource("F5200_001");
                                    return;
                                }
                                if (string.IsNullOrEmpty(invMoves.ToLoc) && string.IsNullOrEmpty(invMoves.ToLpn))
                                {
                                    this.lblMsg.Text = base.GetGlobalMsgResource("F5200_002");
                                    return;
                                }
                                invMovesInfo.Add(invMoves);
                            }
                            else
                            {
                                this.lblMsg.Text = base.GetGlobalMsgResource("F5200_003");
                                return;
                            }
                        }
                    }
                    if ((invMovesInfo != null) && (invMovesInfo.Count > 0))
                    {
                        if (!string.IsNullOrEmpty(InventoryMgtServiceClientProxy.GetInstance().ExecuteInvMoves(invMovesInfo)))
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F5200_004");
                        }
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F5200_004");
                        this.lblMsg.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                this.BindData2MovementList(this.GetInvLotLocLpnQueryEntity());
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnQuery = this.GetInvLotLocLpnQueryEntity();
            if (int.Parse(invLotLocLpnQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                invLotLocLpnQuery.CurrentPage = (int.Parse(invLotLocLpnQuery.CurrentPage) + 1).ToString();
                this.BindData2MovementList(invLotLocLpnQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnQuery = this.GetInvLotLocLpnQueryEntity();
            if (int.Parse(invLotLocLpnQuery.CurrentPage) > 0)
            {
                invLotLocLpnQuery.CurrentPage = (int.Parse(invLotLocLpnQuery.CurrentPage) - 1).ToString();
                this.BindData2MovementList(invLotLocLpnQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            this.BindData2MovementList(this.GetInvLotLocLpnQueryEntity());
        }

        private InvLotLocLpnQueryEntity GetInvLotLocLpnQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as InvLotLocLpnQueryEntity);
            }
            return (base.PageQueryEntitySession as InvLotLocLpnQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InvLotLocLpnQueryEntity invLotLocLpnQuery = this.GetInvLotLocLpnQueryEntity();
                invLotLocLpnQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2MovementList(invLotLocLpnQuery);
            }
        }

        protected void gvMovement_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5212", this.gvMovement);
        }

        protected void gvMovement_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                ((Image) e.Row.FindControl("imgSelectLocation")).Attributes.Add("onclick", "selectLocation(" + ((TextBox) e.Row.FindControl("txtToLoc")).ClientID + ")");
                ((Image) e.Row.FindControl("imgSelectLpn")).Attributes.Add("onclick", "selectLpn(" + ((TextBox) e.Row.FindControl("txtToLpn")).ClientID + ")");
                ((Image) e.Row.FindControl("imgSelectPack")).Attributes.Add("onclick", string.Concat(new object[] { "selectPack(", ((TextBox) e.Row.FindControl("txtPackID")).ClientID, ",", e.Row.RowIndex, ")" }));
                ((TextBox) e.Row.FindControl("txtPackID")).Attributes.Add("onblur", "setPackIDRowIndex(" + e.Row.RowIndex + ")");
                ((TextBox) e.Row.FindControl("txtMoveQty")).Attributes.Add("onblur", "checkUserQtyData('" + this.gvMovement.DataKeys[e.Row.RowIndex]["AVAILABLE_QTY"].ToString() + "',this)");
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
            }
        }

        protected void gvMovement_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InvLotLocLpnQueryEntity invLotLocLpnQueryEntity = this.GetInvLotLocLpnQueryEntity();
                invLotLocLpnQueryEntity.SortField = e.SortExpression;
                if (invLotLocLpnQueryEntity.SortDirection == "ASC")
                {
                    invLotLocLpnQueryEntity.SortDirection = "DESC";
                }
                else if (invLotLocLpnQueryEntity.SortDirection == "DESC")
                {
                    invLotLocLpnQueryEntity.SortDirection = "ASC";
                }
                this.BindData2MovementList(invLotLocLpnQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            InvLotLocLpnQueryEntity invLotLocLpnQuery = new InvLotLocLpnQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    invLotLocLpnQuery.OwnerID = this.PreviousPage.OwnerID;
                    invLotLocLpnQuery.SkuID = this.PreviousPage.SkuID;
                    invLotLocLpnQuery.Lot = this.PreviousPage.Lot;
                    invLotLocLpnQuery.Loc = this.PreviousPage.Loc;
                    invLotLocLpnQuery.LpnID = this.PreviousPage.Lpn;
                    invLotLocLpnQuery.SkuDescr = this.PreviousPage.SkuDescr;
                    invLotLocLpnQuery.ExternalLot = this.PreviousPage.ExternalLot;
                    invLotLocLpnQuery.VendorID = this.PreviousPage.VendorID;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            invLotLocLpnQuery.Qty = " > 0 ";
            invLotLocLpnQuery.IsGetAll = false;
            invLotLocLpnQuery.CurrentPage = "0";
            invLotLocLpnQuery.PageSize = this.gvMovement.PageSize.ToString();
            invLotLocLpnQuery.SortField = "A.ROWID";
            invLotLocLpnQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvMovement.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetInvLotLocLpnQueryEntity(invLotLocLpnQuery);
            this.BindData2MovementList(invLotLocLpnQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetInvLotLocLpnQueryEntity(InvLotLocLpnQueryEntity invLotLocLpnQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = invLotLocLpnQuery;
            }
            else
            {
                base.PageQueryEntitySession = invLotLocLpnQuery;
            }
        }

        protected void txtPackID_TextChanged(object sender, EventArgs e)
        {
            int rowIndex = int.Parse(this.hdnRowIndex.Value);
            string packID = ((TextBox) this.gvMovement.Rows[rowIndex].FindControl("txtPackID")).Text.Trim();
            base.BindUomList2DDL((DropDownList) this.gvMovement.Rows[rowIndex].FindControl("ddlUomID"), packID);
        }

        public MovementQuery PreviousPage
        {
            get
            {
                return (MovementQuery) base.PreviousPage;
            }
        }
    }
}

